import { getQuestionType,getExamSubject,getExamType,submitQuestions,insertQuestionsType,newType,conditionQuestions,User,IDentity,APi,IAA,VIEW,IVAR} from '@/services';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { IQuestionType,IExamType, ISubject, InewType,ICondition,IUser,ID,Ap,AA,viE,IER} from '@/utils/interface';

export interface QuestionModelState {
    questionType: IQuestionType[],
    subjects: ISubject[],
    examTypes: IExamType [],
    newTypes:InewType[],
    Iconditions:ICondition[],
    IUser:IUser[],
    Id:ID[],
    Aps:Ap[],
    AAS:AA[],
    VIES:viE[],
    IEARS:IER[],

    
}
export interface QuetionModelType {
  namespace: 'addQuestions';
  state: QuestionModelState;
  effects: {
    getQuestionType: Effect;
    getExamSubject: Effect;
    getExamType: Effect;
    submitQuestions:Effect;
    insertQuestionsType:Effect;
    newType:Effect;
    conditionQuestions:Effect;
    User:Effect;
    IDentity:Effect;
    APi:Effect;
    IAA:Effect;
    VIEW:Effect;
    IVAR:Effect;


  };
  reducers: {
    save: Reducer<QuestionModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
}
const Classify: QuetionModelType = {
  namespace: 'addQuestions',
  state: {
    questionType: [],
    subjects: [],
    examTypes: [],
    newTypes:[],
    Iconditions:[],
    IUser:[],
    Id:[],
    Aps:[],
    AAS:[],
    VIES:[],
    IEARS:[]


  },
  // 异步操作，理解为vuex中的action, async/await理解为generator函数的语法糖
  effects: {
    // 获取试题列表
    *getQuestionType({ payload }, { call, put }) {
        let result = yield call(getQuestionType);
        if(result.data){
          yield put({
            type: 'save',
            payload: {questionType: result.data}
          })
        }
    },
    //// 获取所有课程
    *getExamSubject({ payload }, { call, put }) {
        let result = yield call(getExamSubject);
        if (result.data) {
          yield put({
            type: 'save',
            payload: { subjects: result.data }
          })
        }
      },
      //获取所有的试题类型
      *getExamType({ payload }, { call, put }) {
        let result = yield call(getExamType);
        if (result.data) {
          yield put({
            type: 'save',
            payload: { examTypes: result.data }
          })
        }
      },
      //添加试题接口
      *submitQuestions({ payload }, { call, put }) {
        let result = yield call(submitQuestions,payload);
        return result
      },
      //添加试题类型
      *insertQuestionsType({payload},{call,put}){
        let result=yield call(insertQuestionsType,payload);
        return result
      },
      //查看试题的所有试题
      *newType({payload},{call,put}){
        let result=yield call(newType);
        if (result.data) {
          yield put({
            type: 'save',
            payload: { newTypes: result.data }
          })
        }
      },
      //查询
      *conditionQuestions({payload},{call,put}){
        let result=yield call(conditionQuestions,payload);
        if (result.data) {
          yield put({
            type: 'save',
            payload: { newTypes: result.data }
          })
        }
      },
      //展示用户
      *User({payload},{call,put}){
        let result=yield call(User,payload);
        if(result.data){
          yield put({
            type: 'save',
            payload: { IUser: result.data }
          })
        }
      },
      //身份展示
      *IDentity({payload},{call,put}){
        let result=yield call(IDentity,payload);
        if(result.data){
          yield put({
            type: 'save',
            payload: { Id: result.data }
          })
        }
      },
      //展示api接口权限数据
      *APi({payload},{call,put}){
        let result=yield call(APi,payload);
        if(result.data){
          yield put({
            type: 'save',
            payload: { Aps: result.data }
          })
        }
      },
      //展示身份和api权限关系
      *IAA({payload},{call,put}){
        let result=yield call(IAA,payload);
        if(result.data){
          yield put({
            type: 'save',
            payload: { AAS: result.data }
          })
        }
      },
      *VIEW({payload},{call,put}){
        let result=yield call(VIEW,payload);
        if(result.data){
          yield put({
            type: 'save',
            payload: { VIES: result.data }
          })
        }
      },
      *IVAR({payload},{call,put}){
        let result=yield call(IVAR,payload);
        if(result.data){
          yield put({
            type: 'save',
            payload: { IEARS: result.data }
          })
        }
      }
  },
  // 同步操作，理解为vuex中的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
};
export default Classify;
